//公共
import myHeader from "/pages/headerfoot/pages/header.vue"
//公共底部
import Tabbar from "/pages/headerfoot/pages/benefitfooter.vue";
//骨架
import uniSkeleton from "/plugin/skeleton/components/heaven-skeleton/heaven-skeleton.vue";
//公共公益方法
import commonBenefit from '@/common/commonbenefit.js';
//公共方法
import Base from '@/common/base.js';
//微信api
import weChat from '@/lib/wechat.js';
//语言
import Lang from '@/utils/lang.js';
//cache缓存
import Cache from '@/utils/cache.js';

export default {
	components: {
		myHeader,
		Tabbar,
		uniSkeleton,
		
	},
	data() {
		return {
			loading: true, //骨架
			list: {}, //结果
			toptitle: '', //头部
			headerdata: [], //头部信息
			langs: Lang.lang, 
			answerrecord: Lang.lang.answerrecord, //答题记录
			comparisonanswers: Lang.lang.comparisonanswers, //答案对比
			youranswer: Lang.lang.youranswer, //你的答案
			correctanswer: Lang.lang.correctanswer, //正确答案
			noanswer: Lang.lang.noanswer, //未答
			gradingtype: '', //类型
		}
	},
	onShow() {
		var that = this;
		that.ifreglogin();
	},
	 
	onLoad(option) {
		console.log(option);
		var that = this;
		var data = {};
		data['users_project_id'] = option.id;
		if (option.userid) {
			data['user_id'] = option.userid; //用户ID
		} else {
			data['user_id'] = Cache.get('userid'); //用户ID
		}
		commonBenefit.getUserHistoryProjectApiMethod(data).then(res => {
			if (res) {
				//阅读
				if (res.grading_type == 'reading') {
					that.reading(res);
				}
				//正向运算
				if (res.grading_type == 'zxys') {
					that.zxys(res);
				}
				that.list = res; //结果
			
				that.gradingtype = res.grading_type; //类型
				that.loading = false;
			}
		});
	},
	methods: {

		//阅读
		reading(data) {
			var that = this;
			that.toptitle = data.project_name; //头部

			that.headerdata['speed'] = Lang.lang.speed; //速度
			that.headerdata['comprehensionrate'] = Lang.lang.comprehensionrate; //理解率
			that.headerdata['numberanswers'] = Lang.lang.numberanswers; //答题数量
			that.headerdata['correctquantity'] = Lang.lang.correctquantity; //正确数量
			that.headerdata['wordperminute'] = Lang.lang.wordperminute; //字/分钟
			var wordperminuteratio = (parseFloat(data.post_str_length) / parseFloat(data.readingtime)) * 60;
			that.headerdata['wordperminuteratio'] = parseFloat(wordperminuteratio).toFixed(0); //字/分钟
			if (data.answer_all != null) {
				var lengths = data.answer_all.length;
			}
			that.headerdata['numberanswersnumber'] = lengths; //答题数量
			var correctquantitynumber = 0;
			data.answer_all.forEach((item, index) => { //js遍历数组
				if (item.user_answer_select == item.answer_select) {
					correctquantitynumber++;
				}
			});
			that.headerdata['correctquantitynumber'] = correctquantitynumber; //正确数量
			var comprehensionrateatio = (parseFloat(correctquantitynumber) / parseFloat(lengths)) * 100
			comprehensionrateatio = parseFloat(comprehensionrateatio).toFixed(0);
			that.headerdata['comprehensionrateatio'] = comprehensionrateatio; //理解率
		},
		//正向运算
		zxys(data) {
			var that = this;
			that.toptitle = Lang.lang.mentalarithmetic; //快速心算

			that.headerdata['goal'] = Lang.lang.goal; //得分
			that.headerdata['answertime'] = Lang.lang.answertime; //答题用时

			that.headerdata['numberanswers'] = Lang.lang.numberanswers; //答题数量
			that.headerdata['correctquantity'] = Lang.lang.correctquantity; //正确数量

			that.headerdata['accuracy'] = Lang.lang.accuracy; //字/分钟
			var wordperminuteratio = (parseFloat(data.post_str_length) / parseFloat(data.readingtime)) / 60;
			that.headerdata['wordperminuteratio'] = parseFloat(wordperminuteratio).toFixed(0); //字/分钟
			if (data.answer_all != null) {
				var lengths = data.answer_all.length;
			}
			that.headerdata['numberanswersnumber'] = lengths; //答题数量
			var correctquantitynumber = 0;
			data.answer_all.forEach((item, index) => { //js遍历数组
				if (item.user_answer_select == item.answer_select) {
					correctquantitynumber++;
				}
			});
			that.headerdata['correctquantitynumber'] = correctquantitynumber; //正确数量
			var comprehensionrateatio = (parseFloat(correctquantitynumber) / parseFloat(lengths)) * 100;
			comprehensionrateatio = parseFloat(comprehensionrateatio).toFixed(0);
			that.headerdata['comprehensionrateatio'] = comprehensionrateatio; //理解率
		},
		//跳转1
		keepTraining(){
			if(!Cache.get('subjectId')){
				Cache.set('subjectId',17)
			}
			uni.navigateTo({
				url:commonBenefit.automaticurl('projectlist')+"?subjectid="+Cache.get('subjectId')
			})
		},
		//判断是否需要注册或者登录与其他
		async ifreglogin() {
			var that = this;
			//注册
			await Base.globalauthorization();

			//判断学生是否注册
			await commonBenefit.ifStudenter().then(res => {
				if (res) {
					uni.redirectTo({
						url: commonBenefit.automaticurl('studentreg')
					})
				}
			});
		},
	}
}